package org.hawk.gwt.ppc;

import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.dev.util.log.PrintWriterTreeLogger;
/**
 * Logger subsystem for preprocessor.
 * This implementation delegates logging to the GWT
 * {@link PrintWriterTreeLogger}.
 * @author alex.bereznevatiy@gmail.com
 */
public class Log {
	private Log(){}
	
	private static PrintWriterTreeLogger logger;
	private static PrintWriterTreeLogger getLogger() {
		if(logger==null){
			logger = new PrintWriterTreeLogger();
			Configuration conf = Environment.getInstance().getConfiguration();
			logger.setMaxDetail(Type.valueOf(
				conf.get("-logLevel",Type.INFO.name())));
		}
		return logger;
	}
	
	/**
	 * Logs debug message
	 * @param msg
	 */
	public static void debug(Object msg) {
		getLogger().log(Type.DEBUG, String.valueOf(msg));
	}
	/**
	 * @return <code>true</code> if debug level of logging is active.
	 */
	public static boolean isDebugEnabled(){
		return getLogger().isLoggable(Type.DEBUG);
	}
	/**
	 * Logs trace message
	 * @param msg
	 */
	public static void trace(Object msg) {
		getLogger().log(Type.TRACE, String.valueOf(msg));
	}
	/**
	 * Logs info message.
	 * @param message
	 */
	public static void info(Object msg){
		getLogger().log(Type.INFO, String.valueOf(msg));
	}
	/**
	 * Logs warning message.
	 * @param message
	 */
	public static void warn(Object msg){
		getLogger().log(Type.WARN, String.valueOf(msg));
	}
	/**
	 * Logs error message.
	 * @param message
	 */
	public static void error(Object msg){
		getLogger().log(Type.ERROR, String
			.valueOf(msg).replace("\n", "\n\t"));
	}

	/**
	 * Logs error message with exception traces.
	 * @param message
	 * @param throwable
	 */
	public static void error(Object msg, Throwable th){
		getLogger().log(Type.ERROR, String
			.valueOf(msg).replace("\n", "\n\t"),th);
	}

	public static void internalError(String msg, Throwable th) {
		error(msg+"Please report about this exception to " +
		"alex.bereznevatiy@gmail.com to improve the quality of " +
		"GWT preprocessor.\nIf you are using some complex expression " +
		"you may split it on simple parts as temporary solution.\n" +
		"For example:\n" +
		"\tClass.forName(str).getMethod(str2).getClass();\n" +
		"can be split on:\n" +
		"\tClass<?> cls = Class.forName(str);\n" +
		"\tMethod m = cls.getMethod(str2);\n" +
		"\tClass<?> cls2 = m.getClass();\n" +
		"Most probably such workaround will help you.\nAnyway do not hesitate " +
		"to contact alex.bereznevatiy@gmail.com for any kind of help.",th);
		
	}
}
